package scau.mis4.chanyulin.atm.view;

import javafx.scene.Cursor;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import scau.mis4.chanyulin.atm.APP;
import scau.mis4.chanyulin.atm.base.common.PATH;
import scau.mis4.chanyulin.atm.base.utils.UiUtils;

/**
 * Created by chanyulin on 2018/4/28.
 * 出钞机图形
 */
public class CashierView extends ImageView {

    // 图片资源
    public final Image CASHIER_EMPTY_IMG = UiUtils.getImage(PATH.CASHIER_EMPTY_IMG_PATH);
    public final Image CASHIER_EJECTED_IMG = UiUtils.getImage(PATH.CASHIER_EJECT_IMG_PATH);

    public CashierView() {
        this.setImage(CASHIER_EMPTY_IMG);
        this.setCursor(Cursor.HAND);
        this.setOnMouseClicked(e -> {
            takeMoneyRequest();
        });
    }

    /**
     * 放入现金
     * @return
     */
    public void putMoneyRequest(int moneyPut) {
        APP.ATM.putMoneyHandler(moneyPut);
        this.setImage(CASHIER_EJECTED_IMG);
    }

    /**
     * 取钱
     * @return
     */
    public void takeMoneyRequest() {
        if (!ableToTakeMoney()) return;

        int moneyTake = APP.ATM.takeMoneyHandler();
        APP.customerBagView.addMoney(moneyTake);
        this.setImage(CASHIER_EMPTY_IMG);
    }

    /**
     * 取款成功后回调此方法
     */
    public void withdrawSuccessCallBack() {
        this.setImage(CASHIER_EJECTED_IMG);
        this.setOnMouseClicked(e -> {
            int moneyTake = APP.ATM.takeMoneyHandler();
            if (moneyTake > 0) {
                this.ejectMoneySuccessCallBack();
                APP.customerBagView.addMoney(moneyTake);
            }
        });
    }

    /**
     * 存款清点钞票成功后回调此方法
     */
    public void countMoneySuccessCallBack() {
        this.setImage(CASHIER_EMPTY_IMG);
    }

    /**
     * 出钞成功回调此方法
     */
    public void ejectMoneySuccessCallBack() {
        this.setImage(CASHIER_EJECTED_IMG);
    }

    /**
     * 判断是否可以取钱
     * @return
     */
    public boolean ableToTakeMoney() {
        return CASHIER_EJECTED_IMG == this.getImage();
    }

}
